import type { RouteRecordRaw } from "vue-router"
import { routerConfig } from "@/router/config"
import { registerNavigationGuard } from "@/router/guard"
import { createRouter } from "vue-router"
import { flatMultiLevelRoutes } from "./helper"

const Layouts = () => import("@/layouts/index.vue")

/**
 * @name 常驻路由
 * @description 除了 redirect/403/404/login 等隐藏页面，其他页面建议设置唯一的 Name 属性
 */
export const constantRoutes: RouteRecordRaw[] = [
  {
    path: "/redirect",
    component: Layouts,
    meta: {
      hidden: true
    },
    children: [
      {
        path: ":path(.*)",
        component: () => import("@/pages/redirect/index.vue")
      }
    ]
  },
  {
    path: "/403",
    component: () => import("@/pages/error/403.vue"),
    meta: {
      hidden: true
    }
  },
  {
    path: "/404",
    component: () => import("@/pages/error/404.vue"),
    meta: {
      hidden: true
    },
    alias: "/:pathMatch(.*)*"
  },
  {
    path: "/login",
    component: () => import("@/pages/login/index.vue"),
    meta: {
      hidden: true
    }
  },
  {
    path: "/",
    component: Layouts,
    redirect: "/dashboard",
    children: [
      {
        path: "dashboard",
        component: () => import("@/views/Dashboard.vue"),
        name: "Dashboard",
        meta: {
          title: "控制台",
          svgIcon: "dashboard",
          affix: true
        }
      }
    ]
  },
  {
    path: '/device',
    name: 'DeviceManage',
    component: Layouts,
    meta: { title: '资产与设备管理', svgIcon: 'device' },
    children: [
      {
        path: 'asset',
        name: 'AssetLedger',
        component: () => import('@/views/DeviceManage/AssetLedger.vue'),
        meta: { title: '资产台账' }
      },
      {
        path: 'profile/:id',
        name: 'DeviceProfile',
        component: () => import('@/views/DeviceManage/DeviceProfile.vue'),
        meta: { title: '设备档案' }
      },
      {
        path: 'department',
        name: 'DepartmentManage',
        component: () => import('@/views/DeviceManage/DepartmentManage.vue'),
        meta: { title: '部门分配管理' }
      }
    ]
  },
  {
    path: '/material',
    name: 'MaterialManage',
    component: Layouts,
    meta: { title: '物料与流转管理', svgIcon: 'material' },
    children: [
      {
        path: 'type',
        name: 'MaterialType',
        component: () => import('@/views/MaterialManage/MaterialType.vue'),
        meta: { title: '物料类型管理' }
      },
      {
        path: 'unit',
        name: 'UnitType',
        component: () => import('@/views/MaterialManage/UnitType.vue'),
        meta: { title: '度量单位管理' }
      },
      {
        path: 'info',
        name: 'MaterialInfo',
        component: () => import('@/views/MaterialManage/MaterialInfo.vue'),
        meta: { title: '物料信息管理' }
      },
      {
        path: 'flow',
        name: 'MaterialFlow',
        component: () => import('@/views/MaterialManage/MaterialFlow.vue'),
        meta: { title: '物料流转记录' }
      },
      {
        path: 'wip',
        name: 'WipManage',
        component: () => import('@/views/MaterialManage/WipManage.vue'),
        meta: { title: '在制品管理' }
      },
      {
        path: 'station',
        name: 'StationHistory',
        component: () => import('@/views/MaterialManage/StationHistory.vue'),
        meta: { title: '过站历史' }
      }
    ]
  },
  {
    path: '/workorder',
    name: 'WorkOrderManage',
    component: Layouts,
    meta: { title: '修复与保养', svgIcon: 'workorder' },
    children: [
      {
        path: 'repair-order',
        name: 'RepairOrder',
        component: () => import('@/views/WorkOrderManage/RepairOrder.vue'),
        meta: { title: '设备修复工单' }
      },
      {
        path: 'repair-plan',
        name: 'RepairPlan',
        component: () => import('@/views/WorkOrderManage/RepairPlan.vue'),
        meta: { title: '修复计划' }
      },
      {
        path: 'fault-report',
        name: 'FaultReport',
        component: () => import('@/views/WorkOrderManage/FaultReport.vue'),
        meta: { title: '故障报修' }
      },
      {
        path: 'maintain-order',
        name: 'MaintainOrder',
        component: () => import('@/views/WorkOrderManage/MaintainOrder.vue'),
        meta: { title: '保养工单' }
      },
      {
        path: 'maintain-plan',
        name: 'MaintainPlan',
        component: () => import('@/views/WorkOrderManage/MaintainPlan.vue'),
        meta: { title: '保养计划' }
      }
    ]
  },
  {
    path: '/green',
    name: 'GreenPerfManage',
    component: Layouts,
    meta: { title: '绿色性能评估与提升', svgIcon: 'green' },
    children: [
      {
        path: 'result',
        name: 'GreenResultView',
        component: () => import('@/views/GreenPerfManage/ResultView.vue'),
        meta: { title: '评估结果展示' }
      },
      {
        path: 'strategy',
        name: 'GreenStrategyGenerator',
        component: () => import('@/views/GreenPerfManage/StrategyGenerator.vue'),
        meta: { title: '提升策略智能生成' }
      },
      {
        path: 'param',
        name: 'GreenParamConfig',
        component: () => import('@/views/GreenPerfManage/ParamConfig.vue'),
        meta: { title: '评估参数配置' }
      }]
  },
  {
    path: '/eco',
    name: 'EcoRepairManage',
    component: Layouts,
    meta: { title: '生态修复与循环利用', svgIcon: 'EcoRepairManage' },
    children: [
      {
        path: 'progress',
        name: 'EcoProgressView',
        component: () => import('@/views/EcoRepairManage/ProgressView.vue'),
        meta: { title: '项目进展展示' }
      },
      {
        path: 'effect',
        name: 'EcoEffectOverview',
        component: () => import('@/views/EcoRepairManage/EffectOverview.vue'),
        meta: { title: '修复成效总览' }
      },
      {
        path: 'problem',
        name: 'EcoProblemView',
        component: () => import('@/views/EcoRepairManage/ProblemView.vue'),
        meta: { title: '存在问题及风险' }
      },
      {
        path: 'region',
        name: 'EcoRegionMap',
        component: () => import('@/views/EcoRepairManage/RegionMap.vue'),
        meta: { title: '修复区域分布' }
      },
      {
        path: 'gantt',
        name: 'EcoGanttChart',
        component: () => import('@/views/EcoRepairManage/GanttChart.vue'),
        meta: { title: '修复计划甘特图' }
      }
    ]
  },
  {
    path: '/stats',
    name: 'StatsAnalysis',
    component: Layouts,
    meta: { title: '数据统计与分析', svgIcon: 'stats' },
    children: [
      {
        path: 'overview',
        name: 'StatsOverview',
        component: () => import('@/views/StatsAnalysis/Overview.vue'),
        meta: { title: '业务总览' }
      },
      {
        path: 'bar',
        name: 'StatsBarChart',
        component: () => import('@/views/StatsAnalysis/BarChart.vue'),
        meta: { title: '柱状图分析' }
      },
      {
        path: 'pie',
        name: 'StatsPieChart',
        component: () => import('@/views/StatsAnalysis/PieChart.vue'),
        meta: { title: '饼图分析' }
      },
      {
        path: 'trend',
        name: 'StatsTrendAnalysis',
        component: () => import('@/views/StatsAnalysis/TrendAnalysis.vue'),
        meta: { title: '趋势对比' }
      }
    ]
  },
  {
    path: '/knowledge',
    name: 'KnowledgeBase',
    component: Layouts,
    meta: { title: '经验知识库', svgIcon: 'knowledge' },
    children: [
      {
        path: 'fault',
        name: 'FaultExperience',
        component: () => import('@/views/KnowledgeBase/FaultExperience.vue'),
        meta: { title: '故障经验库' }
      },
      {
        path: 'cases',
        name: 'CaseCollection',
        component: () => import('@/views/KnowledgeBase/CaseCollection.vue'),
        meta: { title: '案例集锦' }
      },
      {
        path: 'sop',
        name: 'SOPLibrary',
        component: () => import('@/views/KnowledgeBase/SOPLibrary.vue'),
        meta: { title: '标准作业方法' }
      }
    ]
  },
    {
    path: '/standard',
    name: 'StandardManage',
    component: Layouts,
    meta: { title: '规范标准管理', svgIcon: 'standard' },
    children: [
      {
        path: 'maintenance',
        name: 'MaintenanceStandard',
        component: () => import('@/views/StandardManage/MaintenanceStandard.vue'),
        meta: { title: '保养规范' }
      },
      {
        path: 'repair',
        name: 'RepairStandard',
        component: () => import('@/views/StandardManage/RepairStandard.vue'),
        meta: { title: '修复规范' }
      },
      {
        path: 'operation',
        name: 'OperationStandard',
        component: () => import('@/views/StandardManage/OperationStandard.vue'),
        meta: { title: '操作规范' }
      }
    ]
  }
]

/**
 * @name 动态路由
 * @description 用来放置有权限 (Roles 属性) 的路由
 * @description 必须带有唯一的 Name 属性
 */
export const dynamicRoutes: RouteRecordRaw[] = [

]

/** 路由实例 */
export const router = createRouter({
  history: routerConfig.history,
  routes: routerConfig.thirdLevelRouteCache ? flatMultiLevelRoutes(constantRoutes) : constantRoutes
})

/** 重置路由 */
export function resetRouter() {
  try {
    // 注意：所有动态路由路由必须带有 Name 属性，否则可能会不能完全重置干净
    router.getRoutes().forEach((route) => {
      const { name, meta } = route
      if (name && meta.roles?.length) {
        router.hasRoute(name) && router.removeRoute(name)
      }
    })
  } catch {
    // 强制刷新浏览器也行，只是交互体验不是很好
    location.reload()
  }
}

// 注册路由导航守卫
registerNavigationGuard(router)
